from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d, has_prize=True):
        super().__init__(d, has_prize)

    def get_name(self):
        return "行侠仗义五千年"

    def game_alert(self):
        self.alert_close_by_file('./game/img/行侠仗义五千年')

    def swipe_down(self, duration=0.5):
        """
        向下滑半屏
        :return:
        """
        self.log_msg("向下滑半屏")
        w, h = self.d.window_size()
        self.d.swipe(w / 2, h / 2, w / 2, h - 100, duration)

    def game_play(self):
        select_count = 0
        while True:
            if self.img_exit('./game/img/行侠仗义五千年/play_done.png', 2):
                self.log_msg("通关")
                break

            if self.img_exit('./game/img/行侠仗义五千年/play_alert.png', 2):
                self.log_msg(f"选择技能-{select_count}")
                select_count += 1
                if self.img_exit('./game/img/行侠仗义五千年/play_02.png', 2):
                    self.log_msg("有伏波琴，优先选择")
                    self.img_click('./game/img/行侠仗义五千年/play_02.png')
                    continue
                if self.img_exit('./game/img/行侠仗义五千年/play_03.png', 2):
                    self.log_msg("五毒针，优先选择")
                    self.img_click('./game/img/行侠仗义五千年/play_03.png')
                    continue
                self.log_msg("随意选择")
                self.click_top(0.3)

            if select_count <= 8:
                duration = 0.3
                self.swipe_left(duration)
                self.swipe_right(duration)
                self.swipe_right(duration)
                self.swipe_left(duration)
                self.swipe_up(duration)
                self.swipe_down(duration)
                self.swipe_down(duration)
                self.swipe_up(duration)
            else:
                self.log_msg("战斗中", False)
                self.d.sleep(6)

        self.click_bottom()
        self.game_alert()

    def handle_main(self):
        self.log_msg("等待进入游戏主界面")
        if not self.img_exit('./game/img/行侠仗义五千年/game_main.png', 50):
            return False
        self.log_msg("已进入游戏主界面")

        self.game_alert()

        if not self.img_exit('./game/img/行侠仗义五千年/play_01.png', 3):
            self.log_msg("游戏主界面不是桃花谷")
            self.log_msg("点击选择关卡")
            self.click_top(0.5)
            while not self.img_exit('./game/img/行侠仗义五千年/play_01.png', 2):
                self.swipe_right(0.2)

            self.log_msg("找到桃花谷，点击选择")
            self.img_click('./game/img/行侠仗义五千年/play_select.png')
        self.log_msg("点击关卡1")
        self.img_click('./game/img/行侠仗义五千年/play_guan_01.png')

        self.log_msg("点击挑战")
        self.img_click('./game/img/行侠仗义五千年/play_start.png')
        self.log_msg("等待进入战斗界面")
        if not self.img_exit('./game/img/行侠仗义五千年/play_main.png', 50):
            return False
        self.log_msg("已进入战斗界面")

        self.game_play()

        return True
